#include "menubutton.h"
#include <QDebug>

MenuButton::MenuButton(const QString &normalPicName,
                       const QString &activePicName,
                       const QString &highlightPicName,
                       QGraphicsScene *scene,
                       QGraphicsItem *parent,
                       const QSize &size) :
    ImageItem(normalPicName, scene, parent, size),
    activeName(activePicName),
    highlightName(highlightPicName)
{
    state = NORMAL;
    if (size.isEmpty())
    {
        activePixmap = QPixmap(activePicName);
        highlightPixmap = QPixmap(highlightPicName);
    }
    else
    {
        activePixmap = QPixmap(activePicName).scaled(size);
        highlightPixmap = QPixmap(highlightPicName).scaled(size);
    }
    this->setAcceptHoverEvents(true);
}

void MenuButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    switch (state)
    {
    case ACTIVE: painter->drawPixmap(QPointF(), activePixmap);break;
    case HIGHLIGHT: painter->drawPixmap(QPointF(), highlightPixmap);break;
    case NORMAL:
    default: /*painter->drawEllipse(0,0,20,20);break;*/painter->drawPixmap(QPointF(), image);break;
    }
}

void MenuButton::hoverEnterEvent(QGraphicsSceneHoverEvent *)
{
//    qDebug() << "hover" << endl;
    state = HIGHLIGHT;
    update(QRectF(QPointF(), image.size()));
}

void MenuButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
    state = NORMAL;
    update(QRectF(QPointF(), image.size()));
}

void MenuButton::mousePressEvent(QGraphicsSceneMouseEvent *)
{
    state = ACTIVE;
    update(QRectF(QPointF(), image.size()));
}

void MenuButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    state = HIGHLIGHT;
    update(QRectF(QPointF(), image.size()));
    emit clicked(this);
}
